package com.bonc.ioc.service.impl;

import com.bonc.ioc.base.service.BaseService;
import com.bonc.ioc.dao.StandardBasicDataMapper;
import com.bonc.ioc.dao.StandardBasicDataOptionMapper;
import com.bonc.ioc.exceptions.McpException;
import com.bonc.ioc.service.StandardBasicDataOptionService;
import com.bonc.ioc.vo.StandardBasicDataOptionVo;
import com.bonc.ioc.vo.StandardBasicDataVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @description:
 * @author: wangqinglin
 * @date: 2021/3/26 14:20
 * @change : 2021/3/26 14:20 by q8541q@163.com  for init
 */
@Service
public class StandardBasicDataOptionServiceImpl extends BaseService implements StandardBasicDataOptionService {

    @Resource
    private StandardBasicDataOptionMapper standardBasicDataOptionMapper;

    @Resource
    private StandardBasicDataMapper standardBasicDataMapper;

    @Override
    public int deleteByPrimaryKey(String id) {
        return standardBasicDataOptionMapper.delete(id);
    }

    @Override
    public int save(StandardBasicDataOptionVo record) throws McpException {
        StandardBasicDataVo standardBasicDataVo = standardBasicDataMapper.selectByPrimaryKey(record.getBdId());
        String limits = standardBasicDataVo.getLimits();
        if (record.getName().trim().length() > Integer.valueOf(limits)) {
            throw new McpException("选项输入内容超过限制长度");
        }
        fillBasicTimeInfo(record);
        fillModifyTimeInfo(record);
        return standardBasicDataOptionMapper.save(record);
    }

    @Override
    public StandardBasicDataOptionVo selectByPrimaryKey(Long id) {
        return standardBasicDataOptionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(StandardBasicDataOptionVo record) {
        return standardBasicDataOptionMapper.updateByPrimaryKey(record);
    }

    private void fillModifyTimeInfo(StandardBasicDataOptionVo record) {
        record.setModifyTime(new Date());
    }

    private void fillBasicTimeInfo(StandardBasicDataOptionVo record) {
        record.setCreateTime(new Date());
    }

    @Override
    public List<Map> getBasicFieldOptionInfoById(String id) {
        return standardBasicDataOptionMapper.getBasicFieldOptionInfoById(id);
    }
}

